/*
 * QTeXPreview
 * Copyright (C) 2008, Philip Pronin (philip.pronin@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "toolBoxTabConfig.h"

#include <QStandardItemModel>
#include <QDataWidgetMapper>

ToolBoxTabConfig::ToolBoxTabConfig(QWidget* parent) : QDialog(parent) {
	setupUi(this);

	mapper = new QDataWidgetMapper();
	mapper->setModel(listViewEx->getModel());
	mapper->addMapping(lineEditHint, 0);
	mapper->addMapping(texEditorCommand, 1, "text");
	mapper->addMapping(spinBoxCursorPos, 2);
	mapper->addMapping(texEditorImageTeX, 3, "text");

	connect(listViewEx->getSelectionModel(),
		SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
		SLOT(selectionChanged(const QItemSelection &)));
	connect(listViewEx, SIGNAL(beforeUpdate()), mapper, SLOT(submit()));
}

void ToolBoxTabConfig::addCommand(const ToolBoxCommand& cmd) {
	QList<QStandardItem*> itemList;
	QStandardItem* headItem = new QStandardItem(cmd.title);
	headItem->setCheckable(true);
	headItem->setCheckState(cmd.active ? Qt::Checked : Qt::Unchecked);
	itemList << headItem;
	itemList << new QStandardItem(cmd.tex);
	itemList << new QStandardItem(QString::number(cmd.cursor));
	itemList << new QStandardItem(cmd.image);
	listViewEx->getModel()->appendRow(itemList);
}

void ToolBoxTabConfig::addItem() {
	addCommand(ToolBoxCommand(tr("[new]")));

	QStandardItemModel* model = listViewEx->getModel();
	listViewEx->getSelectionModel()->setCurrentIndex(model->index(model->rowCount() - 1, 0),
		QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}

void ToolBoxTabConfig::selectionChanged(const QItemSelection& selected) {
	QItemSelectionModel* selModel = listViewEx->getSelectionModel();
	groupBox->setEnabled(selModel->currentIndex().isValid());
	mapper->setCurrentIndex(selModel->currentIndex().row());
}

void ToolBoxTabConfig::renderImage() {
	renderAreaImage->setTeX(texEditorImageTeX->text());
}

void ToolBoxTabConfig::commandChanged() {
	spinBoxCursorPos->setMaximum(texEditorCommand->text().size());
}

void ToolBoxTabConfig::autoGenImage() {
	QString cmd = texEditorCommand->text();
	cmd.replace("_{}", "_{\\Huge\\cdot}");
	cmd.replace("^{}", "^{\\Huge\\cdot}");
	cmd.replace("{}", "{\\LARGE a}");
	texEditorImageTeX->setText(cmd);
	mapper->submit();
}

void ToolBoxTabConfig::copyImage() {
	texEditorImageTeX->setText(texEditorCommand->text());
	mapper->submit();
}

void ToolBoxTabConfig::autoGenCursorPos() {
	QString cmd = texEditorCommand->text();
	int pos = 0;
	for (; pos < cmd.size(); pos++) if (pos && cmd[pos - 1] == '{') break;
	spinBoxCursorPos->setValue(pos);
	mapper->submit();
}

ToolBoxCommandList ToolBoxTabConfig::getCommandList() const {
	ToolBoxCommandList list;

	QStandardItemModel* model = listViewEx->getModel();
	for (int i = 0; i < model->rowCount(); i++) {
		ToolBoxCommand cmd(model->item(i, 0)->text(),
			model->item(i, 0)->checkState() == Qt::Checked);
		cmd.tex = model->item(i, 1)->text();
		cmd.cursor = model->item(i, 2)->text().toInt();
		cmd.image = model->item(i, 3)->text();
		list << cmd;
	}

	return list;
}

void ToolBoxTabConfig::setCommandList(ToolBoxCommandList list) {
	QStandardItemModel* model = listViewEx->getModel();
	model->clear();
	foreach(ToolBoxCommand cmd, list) addCommand(cmd);
	if (model->rowCount() > 0)
		listViewEx->getSelectionModel()->setCurrentIndex(model->index(0, 0),
			QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
